//
//  UUIntentOrderDetailVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 01/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUIntentOrderDetailVC.h"
#import "UUIntentOrderManager.h"
#import "UUContentContainerVC.h"
#import "UUIntentOrderDetailBasicInfoVC.h"
#import "UUIntentOrderDetailBidVC.h"
#import "UUIntentOrderDetailActionVC.h"

@interface UUIntentOrderDetailVC () <UUIntentOrderManagerObserver>

@property (nonatomic, strong) UUIntentOrderDetailModel *order;
@property (nonatomic, strong) UUContentContainerVC *contentContainerVC;

@end

@implementation UUIntentOrderDetailVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"报价详情页";
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.intentOrderId != nil);
    
    self.contentContainerVC = [UUContentContainerVC instanceOfAppropriate];
    [self addChildViewController:self.contentContainerVC];
    [self.view addSubview:self.contentContainerVC.view];
    [self.contentContainerVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self reload];
    
    [GetIntentOrderManager() addObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reload {
    ShowIndicator();
    WEAK_SELF();
    [GetIntentOrderManager() asyncFetchIntentOrderDetailWithOrderId:self.intentOrderId
                                                      completeBlock:^(UUIntentOrderDetailModel *orderDetail, NSError *error) {
                                                          HideTipsOnView(weakSelf.view);
                                                          
                                                          if (error == nil) {
                                                              weakSelf.order = orderDetail;
                                                              [weakSelf.contentContainerVC removeAllChildrenVC];
                                                              [weakSelf setupUI];
                                                          } else {
                                                              ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                          }
                                                      }];
}

- (void)setupUI {
    UUIntentOrderDetailBasicInfoVC *basicInfoVC = [UUIntentOrderDetailBasicInfoVC instanceOfAppropriate];
    basicInfoVC.order = self.order;
    [self.contentContainerVC insertChildVC:basicInfoVC];
    
    for (UUIntentOrderBidModel *bidModel in self.order.bids) {
        UUIntentOrderDetailBidVC *bidVC = [UUIntentOrderDetailBidVC instanceOfAppropriate];
        bidVC.bidModel = bidModel;
        [self.contentContainerVC insertChildVC:bidVC];
    }
    
    if (self.order.status == UUIntentOrderStatusProcessing) {
        UUIntentOrderDetailActionVC *actionVC = [UUIntentOrderDetailActionVC instanceOfAppropriate];
        actionVC.orderModel = self.order;
        [self.contentContainerVC insertChildVC:actionVC];
    }
}

#pragma mark - UUIntentOrderManagerObserver methods

- (void)intentOrderDidUpdate:(NSNumber *)orderId withActionType:(NSNumber *)actionType {
    if ([self.intentOrderId isEqual:orderId]) {
        [self reload];
    }
}

@end
